package application.common.ui.controller;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import application.common.render.Archive;
import application.common.render.Criminal;
import application.common.render.CriminalProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import net.sf.json.JSONArray;
public class ArchiveDialogController implements Initializable {

	@FXML
	private Label caseNo;

	@FXML
	private TextArea causeTextArea;

	@FXML
	private TextArea briefCaseTextArea;

	@FXML
	private Label departmentName;

	@FXML
	private Label usernams;

	@FXML
	private Button closeBtn;

	@FXML
	private TableView<CriminalProperty> criminalTableView;

	private final ObservableList<CriminalProperty> data = FXCollections.observableArrayList();

	private ObjectProperty<Archive> archive = new SimpleObjectProperty<Archive>();
	private Stage stage;
	public ArchiveDialogController(Stage stage,Archive archive) {
		this.archive.set(archive);
		this.stage=stage;
	}

	@SuppressWarnings("unchecked")
	public void initialize(URL arg0, ResourceBundle arg1) {
		closeBtn.setOnAction(new EventHandler<ActionEvent>() {
		public void handle(ActionEvent event) {
				stage.close();
			}
		});

		TableColumn<CriminalProperty, String> nameColumn = new TableColumn<CriminalProperty, String>("姓名");
		nameColumn.setMinWidth(150);
		nameColumn.setCellValueFactory(new PropertyValueFactory<CriminalProperty, String>("name"));

		TableColumn<CriminalProperty, String> sfzhColumn = new TableColumn<CriminalProperty, String>("身份证号");
		sfzhColumn.setMinWidth(200);
		sfzhColumn.setCellValueFactory(new PropertyValueFactory<CriminalProperty, String>("sfzh"));

		TableColumn<CriminalProperty, String> statusColumn = new TableColumn<CriminalProperty, String>("进度");
		statusColumn.setMinWidth(200);
		statusColumn.setCellValueFactory(new PropertyValueFactory<CriminalProperty, String>("statusName"));

		TableColumn<CriminalProperty, String> expireDateColumn = new TableColumn<CriminalProperty, String>("到期时间");
		expireDateColumn.setMinWidth(300);
		expireDateColumn.setCellValueFactory(new PropertyValueFactory<CriminalProperty, String>("expireDate"));
		criminalTableView.getColumns().addAll(nameColumn, sfzhColumn, statusColumn, expireDateColumn);

		if (archive != null && archive.get()!=null) {
			caseNo.setText(archive.get().getArchiveNo());
			causeTextArea.setText(archive.get().getReason());
			briefCaseTextArea.setText(archive.get().getBrief());
			departmentName.setText(archive.get().getDepartment());
			usernams.setText(archive.get().getDirectors());
		}

		if (archive != null && archive.get()!=null && archive.get().getCriminals() != null && archive.get().getCriminals().size() > 0) {
			JSONArray jsonarray = JSONArray.fromObject(archive.get().getCriminals());  
			List<Criminal>  criminals= (List<Criminal>)JSONArray.toCollection(jsonarray, Criminal.class); 
			if(criminals!=null) {
				for (Criminal criminal : criminals) {
					data.add(new CriminalProperty(criminal));
				}
			}
		}
		criminalTableView.setItems(data);

	}
}
